package com.robot.member.mq;

import com.alibaba.fastjson.JSON;
import com.robot.member.dao.EvaluateDao;
import com.robot.member.dao.MemberDao;
import com.robot.member.pojo.Evaluate;
import com.robot.member.pojo.Member;


import org.junit.platform.commons.logging.Logger;
import org.junit.platform.commons.logging.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;


@Component
@RabbitListener(queues = "member")
public class MemberListener {
    @Autowired
    private MemberDao memberDao;

    @Autowired
    private EvaluateDao evaluateDao;

    @RabbitHandler
    public void exec(String str) {

        if (str.startsWith("member")) {
            //导入会员
            System.out.println(str);
            Member member = JSON.parseObject(str.replace("member", ""), Member.class);
            //处理码豆
            member.setStatus(member.getStatus() == "1" ? "0" : "1");
            member.setTotalBeans(member.getBeans());
            LocalDateTime now = member.getCreatedAt();
            member.setYear(now.getYear() + "");
            member.setMonth(now.getMonthValue() + "");
            member.setDate(now.getDayOfMonth() + "");
            member.setDay(now.getDayOfWeek().getValue() + "");

            //member.setHour(now.getHour());
            System.out.println(member.toString());
            memberDao.save(member);
        }else if(str.startsWith("evaluate")){
            //导入评论
            System.out.println(str);
            Evaluate evaluate=JSON.parseObject(str.replace("evaluate",""),Evaluate.class);
            evaluate.setUpdatedAt(evaluate.getCreatedAt());
            evaluateDao.save(evaluate);
            System.out.println(evaluate.toString());
        }

    }
}
